#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include <utility>
#include <algorithm>
#include <format>


using namespace std;



int twitter_reverse()
{
    regex twt_pattern{"> [\\d]+"};
    regex h1_pattern{"^# "};
    regex h2_pattern{"^## "};


    ifstream inputFile("Messages copy.md", ios::ate);
    if (!inputFile.good()) {
        std::cerr << "Error: Failed to open the file." << std::endl;
        return 1;
    }

    vector<string>                                                     twitter{};
    pair<string, vector<vector<string>>>                               h2{};
    pair<string, vector<pair<string, vector<vector<string>>>>>         h1{};
    vector<pair<string, vector<pair<string, vector<vector<string>>>>>> buffer{};


    char   x;
    string line{};
    if (inputFile.is_open()) {
        streampos size = inputFile.tellg();
        for (int var = 1; var <= size; var++) {
            inputFile.seekg(-var, ios::end);
            inputFile.get(x);

            if (x == 0) {
                continue;
            }

            if (x == '\n') {
                static bool flag = false;
                if (flag == true) {
                    string templine = line;

                    if (regex_search(templine, twt_pattern)) {
                        twitter.push_back(templine);
                        reverse(twitter.begin(), twitter.end());
                        h2.second.push_back(twitter);
                        line    = {};
                        twitter = {};
                    }
                    else if (regex_search(templine, h2_pattern)) {
                        h2.first = templine;
                        h1.second.push_back(h2);
                        line    = {};
                        twitter = {};
                        h2      = {};
                    }
                    else if (regex_search(templine, h1_pattern)) {
                        h1.first = templine;
                        buffer.push_back(h1);
                        line    = {};
                        twitter = {};
                        h2      = {};
                        h1      = {};
                    }
                    else {
                        twitter.push_back(templine);
                        line = {};
                    }
                }
                flag = !flag;
            }
            else {
                line = x + line;
            }

            x = 0;
        }
        if (line.size() > 0) {
            h1.first = line;
            buffer.push_back(h1);
        }
    }


    inputFile.close();

    if (inputFile.bad()) {
        std::cerr << "Error: An unrecoverable error occurred while reading the file." << std::endl;
        return 1;
    }

    ofstream outputFile("Message reverse.md", ios::out);
    if (outputFile.is_open()) {
        for (auto item : buffer) {
            outputFile << item.first << "\n" << endl;
            for (auto item2 : item.second) {
                outputFile << item2.first << "\n>" << endl;
                for (auto item3 : item2.second) {
                    for (auto item4 : item3) {
                        outputFile << item4 << endl;
                    }
                }
                outputFile << endl;
            }

            outputFile << "<div STYLE=\"page-break-after: always;\"></div>" << "\n"
                       << endl;   //  输出一个HTML标签，用于在PDF中创建分页符，并换行
        }
    }
    else {
        std::cout << "Failed to open the file." << std::endl;   // 如果文件打开失败，输出错误信息
    }

    outputFile.close();   // 关闭文件流

    return 0;
}


int px_halving()
{
    ifstream inputFile("style.less", ios::beg);
    if (!inputFile.good()) {
        std::cerr << "Error: Failed to open the file." << std::endl;
        return 1;
    }

    ofstream outputFile("style_2.less", ios::out);
    if (!outputFile.is_open()) {
        std::cout << "Failed to open the file." << std::endl;   // 如果文件打开失败，输出错误信息
        return 1;
    }

    vector<string> buffer{};

    regex px_pattern{"([\\d]*)px"};

    string line{};
    if (inputFile.is_open()) {
        while (getline(inputFile, line)) {
            string                tempLine = line;
            const sregex_iterator end;
            for (sregex_iterator iter{cbegin(tempLine), cend(tempLine), px_pattern}; iter != end;
                 ++iter)
            {
                int              numb   = stoi((*iter)[1].str());
                int              numb_2 = (int)(numb * 0.5);
                string::iterator pos{begin(tempLine)};

                if (numb >= 1000) {
                    advance(pos, distance<std::string::const_iterator>(pos, (*iter)[1].second - 4));
                    if (numb_2 >= 1000) {
                        *(pos) = numb_2 / 1000 % 10 + '0';
                    }
                    else {
                        *(pos) = ' ';
                    }
                    *(pos + 1) = numb_2 / 100 % 10 + '0';
                    *(pos + 2) = numb_2 / 10 % 10 + '0';
                    *(pos + 3) = numb_2 % 10 + '0';
                }
                else if (numb >= 100) {
                    advance(pos, distance<std::string::const_iterator>(pos, (*iter)[1].second - 3));
                    if (numb_2 >= 100) {
                        *(pos) = numb_2 / 100 % 10 + '0';
                    }
                    else {
                        *(pos) = ' ';
                    }
                    *(pos + 1) = numb_2 / 10 % 10 + '0';
                    *(pos + 2) = numb_2 % 10 + '0';
                }
                else if (numb >= 10) {
                    advance(pos, distance<std::string::const_iterator>(pos, (*iter)[1].second - 2));
                    if (numb_2 >= 10) {
                        *(pos) = numb_2 / 10 % 10 + '0';
                    }
                    else {
                        *(pos) = ' ';
                    }
                    *(pos + 1) = numb_2 % 10 + '0';
                }
                else if (numb > 1) {
                    advance(pos, distance<std::string::const_iterator>(pos, (*iter)[1].second - 1));
                    *(pos) = numb_2 % 10 + '0';
                }

                if (numb > 1) {
                    cout << format("before:{}, after:{}", numb, numb_2) << endl;
                    cout << "line: " << line << endl;
                    cout << "tempLine: " << tempLine << "\n" << endl;
                }
            }
            buffer.push_back(tempLine);
        }
    }

    for (auto iter : buffer) {
        outputFile << iter << endl;
    }

    inputFile.close();
    outputFile.close();   // 关闭文件流

    return 0;
}
